package org.huawei260.TiKu.C.C048;

/**
 * @Auther: qingle
 * @Date: 2024/10/22-17:29
 * @Description:
 * @version: 1.0
 */

import java.util.*;

public class AppUsageSystem {
	static class App {
		String name;
		int priority;
		String startTime;
		String endTime;

		public App(String name, int priority, String startTime, String endTime) {
			this.name = name;
			this.priority = priority;
			this.startTime = startTime;
			this.endTime = endTime;
		}
	}

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = Integer.parseInt(scanner.nextLine().trim());
		List<App> apps = new ArrayList<>();

		for (int i = 0; i < n; i++) {
			String[] parts = scanner.nextLine().split(" ");
			String name = parts[0];
			int priority = Integer.parseInt(parts[1]);
			String startTime = parts[2];
			String endTime = parts[3];
			apps.add(new App(name, priority, startTime, endTime));
		}

		String queryTime = scanner.nextLine().trim();
		String result = "NA";
		int maxPriority = -1;

		for (App app : apps) {
			if (isTimeWithin(queryTime, app.startTime, app.endTime)) {
				if (app.priority > maxPriority) {
					maxPriority = app.priority;
					result = app.name;
				}
			}
		}

		System.out.println(result);
	}

	// 比较时间的方法是错误的
	private static boolean isTimeWithin(String queryTime, String startTime, String endTime) {
		return queryTime.compareTo(startTime) >= 0 && queryTime.compareTo(endTime) < 0;
	}
}
